from google.appengine.ext import db
from horseanalysis.lib.callable import Callable
from horseanalysis.model.race import Race

class RaceBatch(db.Model):
  race = db.ReferenceProperty(Race)
  batch_time = db.DateTimeProperty()
  time_diff = db.IntegerProperty()
  win_pool = db.FloatProperty()
  place_pool = db.FloatProperty()
  quinella_pool = db.FloatProperty()
  quinella_place_pool = db.FloatProperty()
  double_pool = db.FloatProperty()
  tierce_pool = db.FloatProperty()
  trio_pool = db.FloatProperty()
  first_four_pool = db.FloatProperty()
  raw_investment = db.TextProperty()
  raw_win_place = db.TextProperty()
  raw_quinella = db.TextProperty()
  raw_quinella_place = db.TextProperty()
  completed_place_probability_set = db.BooleanProperty()
  completed_quinella_probability_set = db.BooleanProperty()
  completed_quinella_place_probability_set = db.BooleanProperty()
  
  def time_diff_to_race_time(self):
    race_time = self.race.race_time
    diff = self.batch_time - race_time
    if diff.days >= 0:
      # race not started yet
      return "%(day)d days %(min)d mins beyond" % {'day': diff.days, 'min': int(diff.seconds / 60)}
    else:
      diff = race_time - self.batch_time
      return "%d mins to start" % int(diff.seconds / 60)
  
  def update_pool(self, win, place, qin, qpl, dbl, tie, tri, ff):
    self.win_pool = win
    self.place_pool = place
    self.quinella_pool = qin
    self.quinella_place_pool = qpl
    self.double_pool = dbl
    self.tierce_pool = tie
    self.trio_pool = tri
    self.first_four_pool = ff
    self.put()
  
  def update_investment(self, raw_data):
    self.raw_investment = raw_data
    self.put()
    
  def update_win_place(self, raw_data):
    self.raw_win_place = raw_data
    self.put()
  
  def update_quinella(self, raw_data):
    self.raw_quinella = raw_data
    self.put()
  
  def update_quinella_place(self, raw_data):
    self.raw_quinella_place = raw_data
    self.put()
  
  def set_completed_place_probability_set(self, value):
    self.completed_place_probability_set = value
    self.put()
  
  def set_completed_quinella_probability_set(self, value):
    self.completed_quinella_probability_set = value
    self.put()
    
  def set_completed_quinella_place_probability_set(self, value):
    self.completed_quinella_place_probability_set = value
    self.put()
    
  def get_batch(key):
    return RaceBatch.get(key)
  
  def create_or_retrieve(race, time):
    query = RaceBatch.gql("WHERE race = :1 and batch_time = :2", race, time)
    for batch in query:
      return batch
    batch = RaceBatch(race=race, batch_time=time)
    
    race_time = batch.race.race_time
    diff = batch.batch_time - race_time
    if diff.days >= 0:
      pass
      batch.time_diff = (diff.days * 86400) + diff.seconds
    else:
      diff = race_time - batch.batch_time
      batch.time_diff = -1*((diff.days * 86400) + diff.seconds)
    
    batch.put()
    return batch
  
  def get_race_batches(race):
    query = RaceBatch.gql("WHERE race = :1 order by batch_time desc", race)
    return query
  
  def get_latest_batch(race):
    query = RaceBatch.gql("WHERE race = :1 order by batch_time desc limit 1", race)
    for batch in query:
      return batch
    return False
  
  def get_batches_with_remaining_time(miniutes):
    if miniutes == None:      
      query = RaceBatch.all()
      return query
    else:
      query = RaceBatch.all()
      return query
  
  @classmethod
  def get_race_batches_not_completed_probability_set(cls):
    query = cls.gql("WHERE completed_place_probability_set = :1 and completed_quinella_probability_set = :2 and completed_quinella_place_probability_set = :3", None, None, None)
    return query
    
  create_or_retrieve = Callable(create_or_retrieve)
  get_race_batches = Callable(get_race_batches)
  get_latest_batch = Callable(get_latest_batch)
  get_batch = Callable(get_batch)
  get_batches_with_remaining_time = Callable(get_batches_with_remaining_time)